package com.stylefeng.guns.modular.model.controller;

import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CarSite;
import com.stylefeng.guns.modular.model.service.ICarSiteService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

/**
 * model控制器
 *
 * @author fengshuonan
 * @Date 2018-08-01 11:07:44
 */
@Controller
@RequestMapping("/carSite")
public class CarSiteController extends BaseController {

    private String PREFIX = "/model/carSite/";

    @Autowired
    private ICarSiteService carSiteService;

    /*
      所有站点信息
     */
    @RequestMapping("/site")
    public Object site(HttpServletResponse response, HttpServletRequest request) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        List<CarSite> listcar=carSiteService.carlinelist(null);
        Writer out = response.getWriter();
        out.write(JSON.toJSONString(listcar));
        return  null;
    }



    /**
     *
     *
     * 跳转到model首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "carSite.html";
    }

    /**
     * 跳转到添加model
     */
    @RequestMapping("/carSite_add")
    public String carSiteAdd() {
        return PREFIX + "carSite_add.html";
    }

    /**
     * 跳转到修改model
     */
    @RequestMapping("/carSite_update/{carSiteId}")
    public String carSiteUpdate(@PathVariable Integer carSiteId, Model model) {
        CarSite carSite = carSiteService.selectById(carSiteId);
        model.addAttribute("item",carSite);
        LogObjectHolder.me().set(carSite);
        return PREFIX + "carSite_edit.html";
    }

    /**
     * 获取model列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return carSiteService.selectList(null);
    }

    /**
     * 新增model
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CarSite carSite) {
        carSiteService.insert(carSite);
        return SUCCESS_TIP;
    }

    /**
     * 删除model
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer carSiteId) {
        carSiteService.deleteById(carSiteId);
        return SUCCESS_TIP;
    }

    /**
     * 修改model
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CarSite carSite) {
        carSiteService.updateById(carSite);
        return SUCCESS_TIP;
    }

    /**
     * model详情
     */
    @RequestMapping(value = "/detail/{carSiteId}")
    @ResponseBody
    public Object detail(@PathVariable("carSiteId") Integer carSiteId) {
        return carSiteService.selectById(carSiteId);
    }
}
